package com.beehoo.dcportal.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.PortletDao;
import com.beehoo.dcportal.admin.service.PortletService;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.PortletBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class PortletServiceImpl extends BaseService implements PortletService {

	private PortletDao portletDao;

	public void setPortletDao(PortletDao portletDao) {
		this.portletDao = portletDao;
	}

	@Override
	public void changePortlet(String streetId, String themeId,
			String[] portlet) throws BaseException {
		this.validatePortlet(streetId, themeId, portlet);
		int count = portlet.length;
		List<PortletBean> porletList = new ArrayList<PortletBean>();
		for(int i=0;i<count;i++){
			String[] portletTmp = portlet[i].split("~");
			PortletBean portletBean = new PortletBean();
			portletBean.setThemeId(themeId);
			portletBean.setStreetId(streetId);
			portletBean.setPortletOrder(portletTmp[0]);
			portletBean.setPortletPosition(portletTmp[1]);
			portletBean.setPortletTitle(portletTmp[2]);
			portletBean.setPortletPath(portletTmp[3]);
			portletBean.setMsgTypeId(portletTmp[4]);
			portletBean.setDisplayTitle(portletTmp[5]);
			portletBean.setPortletType(portletTmp[6]);
			porletList.add(portletBean);
		}
		portletDao.changePortlet(themeId, porletList);
	}

	@Override
	public List<PortletBean> queryPortlet(String streetId, String themeId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(themeId).equals("")) {
			throw new BaseException("custom.messages.error.theme.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("themeId", themeId);
		return portletDao.queryPortlet(paramMap);
	}

	private void validatePortlet(String streetId, String themeId,
			String[] portlet) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(themeId).equals("")) {
			throw new BaseException("custom.messages.error.theme.id.empty");
		}
		if (portlet == null) {
			throw new BaseException(
					"custom.messages.error.portlet.change.error");
		}
	}

}
